package wiki.mdzz.learn_android.controllers

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import wiki.mdzz.learn_android.R
import wiki.mdzz.learn_android.data.Fruit
import wiki.mdzz.learn_android.data.FruitAdapter
import wiki.mdzz.learn_android.databinding.ActivityFstBinding
import wiki.mdzz.learn_android.utils.BaseActivity

class FstActivity : BaseActivity() {
    private lateinit var binding: ActivityFstBinding

    private val fruitList = ArrayList<Fruit>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFstBinding.inflate(layoutInflater)
        setContentView(binding.root)
        supportActionBar?.hide()
        binding.titleComp.updateTitle("test")
        binding.btn2.setOnClickListener {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("https://www.baidu.com")
            startActivity(intent)
        }
//        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data)
        initFruits()
        val adapter = FruitAdapter(this, R.layout.fruit_item, fruitList)
        binding.listView.adapter = adapter
        binding.listView.setOnItemClickListener { _, _, position, _ ->
            val fruit = fruitList[position]
            Toast.makeText(this, fruit.name, Toast.LENGTH_SHORT).show()
        }
    }

    private fun initFruits() {
        repeat(2) {
            fruitList.add(Fruit("Apple", R.drawable.test))
            fruitList.add(Fruit("Banana", R.drawable.test))
            fruitList.add(Fruit("Orange", R.drawable.test))
            fruitList.add(Fruit("Pear", R.drawable.test))
            fruitList.add(Fruit("Grape", R.drawable.test))
            fruitList.add(Fruit("Pineapple", R.drawable.test))
            fruitList.add(Fruit("Strawberry", R.drawable.test))
            fruitList.add(Fruit("Cherry", R.drawable.test))
            fruitList.add(Fruit("Mango", R.drawable.test))
        }
    }
}